﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperWebSocket;

namespace _21SocketServer
{
    public class _21WebServer : WebSocketServer<_21WebSession>, I21Server
    {
        public I21Session GetSessionByUserName(string username)
        {
            return GetSessions(m => m.UserName == username).SingleOrDefault() as I21Session;
        }

        public List<I21Session> GetAllSession()
        {
            return new List<I21Session>(GetSessions(m => true));
        }

        protected override void OnNewSessionConnected(_21WebSession session)
        {
            base.OnNewSessionConnected(session);
            lock (session.Server.Game)
            {
                if (session.Server.Game.IsStart)
                    session.Command("ERROR 牌局已经开始，请稍后！");
                else
                    session.Server.Game.SendPlayerInfo(session);
            }
        }

        protected override void OnStartup()
        {
            base.OnStartup();
            Game = new Game();
            Game.Server = this;
        }

        public Game Game { get; set; }
    }
}
